package com.shop.cereshop.app.dl.logic.utils;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shop.cereshop.app.dl.logic.utils.YunUtil.Utils;
import com.shop.cereshop.app.redis.service.api.StringRedisService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: dlshop
 * 描述：
 * @author: 我好帅
 * @create: 2021-07-28 14:50
 **/
@Slf4j
public class GetAccessToken2 {
    public static String AccessTokenAndOpenId(String appid, String backendToken, String code, StringRedisService redis) {
//        if (redis.get("YunAccessToken") == null) {
//        if (redis.get("YunOpenId") == null) {
            //调用地址
            String url = "https://open.95516.com/open/access/1.0/token";

            //拼装参数Map
            Map<String, String> params = new HashMap<String, String>(0);
            params.put("appId", appid);
            params.put("backendToken", backendToken);

            //登录获取的code
            params.put("code", code);

            //接入方直接填写常量字符串authorization_code
            params.put("grantType", "authorization_code");

            //发送请求
            String resultStr = Utils.sendPostGson(url, params);
            log.info("-------获取accessToken和openId:" + resultStr + "---------");
            JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
//            String t = jsonObject.get("params").getAsJsonObject().get("accessToken").getAsString();
//            Integer time = jsonObject.get("params").getAsJsonObject().get("expiresIn").getAsInt();
//            time *= 1000;
            String openid = jsonObject.get("params").getAsJsonObject().get("openId").getAsString();
//            redis.set("YunAccessToken", t, time);
            return openid;
//        } else {
////            return String.valueOf(redis.get("YunAccessToken"));
//            return String.valueOf(redis.get("YunAccessToken"));
//        }
    }
}
